package com.example.nequimessage;

import android.app.Notification;

import android.os.Bundle;

import android.service.notification.NotificationListenerService;

import android.service.notification.StatusBarNotification;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;

public class NotificationMonitorService1 extends NotificationListenerService {

    // 在收到消息时触发

    @Override

    public void onNotificationPosted(StatusBarNotification sbn) {

        // TODO Auto-generated method stub

        Bundle extras = sbn.getNotification().extras;

        // 获取接收消息APP的包名

        String notificationPkg = sbn.getPackageName();

        // 获取接收消息的抬头

        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);

        // 获取接收消息的内容

        String notificationText = extras.getString(Notification.EXTRA_TEXT);

        // com.nequi.MobileApp
        Log.i("11111111111111111111111", System.currentTimeMillis() +" " + notificationPkg);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                String result = HttpUtils.getJsonContent("https://api.apiopen.top/api/sentences");
                try {
                    Log.i("GET", "123456789" + result);
                    JSONObject jsonObject = new JSONObject(result);
                    Log.i("GETjson", "123456789" + jsonObject);
                    System.out.println("jsonObject的数据类型是：" + jsonObject.get("code"));

                    String data = "Hello World!";
                    try {
                        System.out.println("..........");
                        File file = new File("/sdcard/nequi.txt"); // 设置保存路径及文件名
                        System.out.println(file);
                        System.out.println("???????????");
                        if (!file.exists()) {
                            // 如果文件不存在则创建新文件
//                                            file.createNewFile();
                            System.out.println("3333333333333");
                            if (file.createNewFile()){
                                System.out.println("File is created!");
                                //Runtime.getRuntime().exec("chmod 777 /home/test3.txt");
                                file.setExecutable(true);//设置可执行权限
                                file.setReadable(true);//设置可读权限
                                file.setWritable(true);//设置可写权限
                                System.out.println("is execute allow : " + file.canExecute());
                                System.out.println("is read allow : " + file.canRead());
                                System.out.println("is write allow : " + file.canWrite());
                            }else{
                                System.out.println("File already exists.");
                            }
                        }

                        System.out.println("!!!!!!!!！");

                        FileWriter writer = new FileWriter(file, true);
                        writer.write(data+"\r\n"); // 写入数据
                        writer.close(); // 关闭FileWriter对象

                        System.out.println("数据已成功保存到文件！");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Log.i("NotificationInfo", " Notification posted " + notificationTitle +" & " + notificationText);

    }

    // 在删除消息时触发

    @Override

    public void onNotificationRemoved(StatusBarNotification sbn) {

        // TODO Auto-generated method stub

        Bundle extras = sbn.getNotification().extras;

        // 获取接收消息APP的包名

        String notificationPkg = sbn.getPackageName();

        // 获取接收消息的抬头

        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);

        // 获取接收消息的内容

        String notificationText = extras.getString(Notification.EXTRA_TEXT);

        Log.i("NotificationInfo", " Notification removed " + notificationTitle +" & " + notificationText);

    }

}
